package xyz.xtt.asset.service.impl;

import java.util.Collection;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.toolkit.JoinWrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.google.common.collect.Lists;

import cn.hutool.core.collection.CollectionUtil;
import xyz.xtt.asset.client.response.AssetStaticResponse;
import xyz.xtt.asset.constants.AssetCache;
import xyz.xtt.asset.entity.TblAssetStaticResource;
import xyz.xtt.asset.mapper.TblAssetStaticResourceMapper;
import xyz.xtt.asset.service.ITblAssetStaticResourceService;
import xyz.xtt.common.starter.cache.CacheManager;

/**
 * <p>
 * 资产扩展信息表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-10-17
 */
@Service
public class TblAssetStaticResourceServiceImpl extends ServiceImpl<TblAssetStaticResourceMapper, TblAssetStaticResource>
		implements ITblAssetStaticResourceService {
	@Autowired
	private CacheManager cacheManager;

	@Override
	public TblAssetStaticResource info(long assetId) {
		LambdaQueryChainWrapper<TblAssetStaticResource> query = new LambdaQueryChainWrapper<>(baseMapper);
		return query.eq(TblAssetStaticResource::getAssetId, assetId).one();
	}

	@Override
	public List<TblAssetStaticResource> infoWithAssetIds(Collection<Long> assetIds) {
		if (CollectionUtil.isEmpty(assetIds)) {
			return Lists.newArrayList();
		}
		LambdaQueryChainWrapper<TblAssetStaticResource> query = new LambdaQueryChainWrapper<>(baseMapper);
		return query.in(TblAssetStaticResource::getAssetId, assetIds).list();
	}

	@Override
	public TblAssetStaticResource infoWithAssetIdByCache(Long assetId) {
		return cacheManager.cacheObj(() -> info(assetId), AssetCache.TBL_ASSET_STATIC_RESOURCE, String.valueOf(assetId));
	}

	@Override
	public List<AssetStaticResponse> queryBatchAssetStaticResponse(Collection<Long> assetIds) {
		MPJLambdaWrapper<TblAssetStaticResource> query = JoinWrappers.lambda(TblAssetStaticResource.class).in(TblAssetStaticResource::getAssetId, assetIds);
		List<AssetStaticResponse> list = query.list(AssetStaticResponse.class);
		return list;
	}

	@Override
	public AssetStaticResponse queryTblAssetStaticResourceInfo(Long assetId) {
		MPJLambdaWrapper<TblAssetStaticResource> query = JoinWrappers.lambda(TblAssetStaticResource.class).eq(TblAssetStaticResource::getAssetId, assetId);
		AssetStaticResponse one = query.one(AssetStaticResponse.class);
		return one;
	}

	@Override
	public Boolean setCertificateUri(Long assetId, String certificateUri) {
		boolean update = new LambdaUpdateChainWrapper<>(baseMapper)
				.eq(TblAssetStaticResource::getAssetId, assetId)
				.set(TblAssetStaticResource::getCertificateUri, certificateUri)
				.update();
		if (update) {
			cacheManager.delete(AssetCache.TBL_ASSET_STATIC_RESOURCE, String.valueOf(assetId));
		}
		return update;
	}
}
